package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.model.GoodModel;
import com.luck.easyfoodbatch.util.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：LeTuGolf
 * package：com.tongyu.luck.paradisegolf.adapter
 * email：893855882@qq.com
 * data：16/7/27
 */
public class DiscountAdapter extends
        RecyclerView.Adapter<DiscountAdapter.ViewHolder> {
    private List<GoodModel.DataBean.DiscountBean> discounts = new ArrayList<>();
    private LayoutInflater mInflater;
    private Context mContext;
    private String spec;

    public void setSpec(String spec) {
        this.spec = spec;
    }

    public void setDiscounts(List<GoodModel.DataBean.DiscountBean> discounts) {
        this.discounts = discounts;
    }

    public DiscountAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_discount_number, tv_discount;

        public ViewHolder(View view) {
            super(view);
            tv_discount_number = (TextView) view.findViewById(R.id.tv_discount_number);
            tv_discount = (TextView) view.findViewById(R.id.tv_discount);
        }
    }


    @Override
    public int getItemCount() {
        return discounts.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.discount_item,
                viewGroup, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        //itemView 的点击事件
        if (mItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onItemClick(viewHolder.getAdapterPosition(), v);
                }
            });
        }
        return viewHolder;
    }


    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        GoodModel.DataBean.DiscountBean bean = discounts.get(position);
        String count = bean.getCount();
        if (Tools.isNull(count)) {
            count = "0";
        }
        if (!Tools.isNull(spec)) {
            //viewHolder.tv_discount_number.setText(count + spec + "以上");
            viewHolder.tv_discount_number.setText(count + "件" + "以上");
        } else {
            viewHolder.tv_discount_number.setText(count + "件" + "以上");
        }
        String discount = bean.getDiscount();
        if (Tools.isNull(discount)) {
            discount = "0";
        }
        viewHolder.tv_discount.setText(discount + "折");
    }

    protected OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }
}
